﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using DeepEarth.Client.MapControl;
using DeepEarth.Client.MapControl.Layers;
using DeepEarth.Client.Services.Yahoo;
using ExampleControl.Popups;

namespace ExampleControl.Controls
{
    public partial class HorizontialToolBarItems
    {
        private readonly Map _map;

        public HorizontialToolBarItems()
        {
            InitializeComponent();
            _map = Map.GetMapInstance(this);
        }

        private void ToggleFullScreen_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        private void ToolBarMapNorth_Click(object sender, RoutedEventArgs e)
        {
            _map.RotationAngle = 0;
        }

        private void ToolBarRotate_Click(object sender, RoutedEventArgs e)
        {
            _map.RotationAngle += 15;
        }

        private void ToolBarResetMap_Click(object sender, RoutedEventArgs e)
        {
            _map.SetViewFullMap();
        }

        private void ToggleMapMode_Click(object sender, RoutedEventArgs e)
        { 
            switch (_map.DragMode)
            {
                case DragBehavior.Select:
                    _map.DragMode = DragBehavior.Pan;
                    ToolTipService.SetToolTip(ToggleMapMode, "Change to Select Mode");
                    if (ToggleDragModeImage != null)
                    {
                        ToggleDragModeImage.Source = new BitmapImage(new Uri("../Resources/ToolBar/Pan.png", UriKind.Relative));
                    }
                    break;

                case DragBehavior.Pan:
                    _map.DragMode = DragBehavior.Select;
                    ToolTipService.SetToolTip(ToggleMapMode, "Change to Pan Mode");
                    if (ToggleDragModeImage != null)
                    {
                        ToggleDragModeImage.Source = new BitmapImage(new Uri("../Resources/ToolBar/Bounds.png", UriKind.Relative));
                    }
                    break;
            }
        }

        private TileLayer _tl;
        private bool _overlayerMode;

        private void ToggleOverlay_Click(object sender, RoutedEventArgs e)
        {
            _overlayerMode = !_overlayerMode;
            switch (_overlayerMode)
            {
                case true:
                    ToolTipService.SetToolTip(ToggleMapMode, "Change to Overlay Mode");
                    if (ToggleOverlayerImage != null)
                    {
                        ToggleOverlayerImage.Source = new BitmapImage(new Uri("../Resources/ToolBar/Overlay.png", UriKind.Relative));
                        if (_tl == null)
                        {
                            _tl = new TileLayer(_map) { Opacity = 0.3D, Source = new YhooTileSource(YhooMapModes.YahooStreet) };
                        }
                        _map.Layers.Add(_tl);
                    }
                    break;

                case false:
                    ToolTipService.SetToolTip(ToggleMapMode, "Change to Map Mode");
                    if (ToggleOverlayerImage != null)
                    {
                        ToggleOverlayerImage.Source = new BitmapImage(new Uri("../Resources/ToolBar/Map.png", UriKind.Relative));

                        if (_tl != null)
                        {
                            _map.Layers.Remove(_tl);
                        }
                    }
                    break;
            }
        }

        private void PrintMap_Click(object sender, RoutedEventArgs e)
        {
            var printPreview = new PrintPreviewDialog(_map)
            {
                previewImage =
                {
                    Width = 400,
                    Height = 300,
                    Source = new WriteableBitmap(_map.BaseLayer.Msi, null)
                }
            };

            printPreview.Show();
        }
    }
}